/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dahg.project.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.dahg.project.controller.model.Categoria;
import org.dahg.project.web.ProductosBean;

/**
 *
 * @author dahg
 */
@FacesConverter(forClass = Categoria.class)
public class CategoriaConverter implements Converter {
    
    
     public ProductosBean getService(String beanName) {
        String elExpression=String.format("#%s%s%s","{",beanName,"}");              
        FacesContext ctx= FacesContext.getCurrentInstance();
        ProductosBean bean = (ProductosBean) ctx.getApplication().evaluateExpressionGet(ctx, elExpression, Object.class);
        return bean;
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Integer id = Integer.valueOf(value);
        return getService("productosBean").getCategoriaService().getById(id);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Categoria c = (Categoria) value;
        return c.getId().toString();
    }

}
